package de.hauck.rtmidlet;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Displayable;

import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockStore;
import de.hauck.rtmidlet.views.FormDebug;
import de.hauck.rtmidlet.views.FormSettingsMenu;

public class FileSystemShowLogThread implements Runnable {

	private Configuration c;
	private StockStore _myStore;
	private Displayable _dble;
	private FormSettingsMenu _formSettings;
	private RTMidlet _main;
	private String _path;


	public FileSystemShowLogThread(String path, Displayable dble) {
		_dble = dble;
		_path=path;
		new Thread(this).start();
	}

	public void run() {

		try {
			FileConnection fc = (FileConnection) Connector.open(_path, //c.exportURL,
					Connector.READ);
			if (!fc.exists()) {
				RTMidlet.getInstance().alert("Error1", "", Configuration.ALERT_WAIT_TIME);
				throw new IOException("File does not exist");
				
			}
			DataInputStream is = fc.openDataInputStream();
			try {
			
				StringBuffer b = new StringBuffer();
				int ch;
				while ((ch = is.read()) != -1) {
					b.append((char) ch);
				}
				System.out.println(b);
				((FormDebug)_dble).showTrace(b.toString());
				
				
			} catch (Exception e) {
				e.printStackTrace();
				RTMidlet.getInstance().alert("Error2", e.getMessage(), Configuration.ALERT_WAIT_TIME);
			} finally {
				if (is != null) {
					is.close();
				}
				if (fc != null) {
					fc.close();
				}
			}
		} catch (IOException e) {
			RTMidlet.getInstance().alert("Error3", e.getMessage(), Configuration.ALERT_WAIT_TIME);
			e.printStackTrace();
		}
		
	}

	
}
